package com.android.kmlmrg.dao;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.util.Log;

import com.google.android.maps.GeoPoint;

public class AddressDao {

	/**LOG**/
	private static final  String LOG_TAG = AddressDao.class.getName();

	public Address getLatitudeAndLongitudeFromGoogleMapForAddress(  Context pContext, String searchedAddress){

		Geocoder coder = new Geocoder( pContext );
		List<Address> address;
		try {
			address = coder.getFromLocationName(searchedAddress,5);
	
		if (address == null) {
			Log.d(LOG_TAG, "############Address not correct #########");
			return null;
		} else if (!address.isEmpty()) {
			Log.d(LOG_TAG, "############Address correct #########");
			return address.get(0);
		} else {
			return null;
		}
		} catch (IOException e) {
			Log.d(LOG_TAG, "############Address not correct #########");
			e.printStackTrace();
		}
		
		return null;
		
	
	}
	
	public  JSONObject getLocationInfo(String address) {

        HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?address=" +address+"&ka&sensor=false");
        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        StringBuilder stringBuilder = new StringBuilder();

        try {
            response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            InputStream stream = entity.getContent();
            int b;
            while ((b = stream.read()) != -1) {
                stringBuilder.append((char) b);
            }
        } catch (ClientProtocolException e) {
        } catch (IOException e) {
        }

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject = new JSONObject(stringBuilder.toString());
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return jsonObject;
    }


	public  GeoPoint getLatitudeAndLongitudeFromGoogleMapForAddress(String searchedAddress) {

    	JSONObject jsonObject = getLocationInfo( searchedAddress );
    	
    	
    	
    	Double lon = new Double(0);
        Double lat = new Double(0);

        try {

            lon = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
                .getJSONObject("geometry").getJSONObject("location")
                .getDouble("lng");

            lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
                .getJSONObject("geometry").getJSONObject("location")
                .getDouble("lat");

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));
     

    }



}
